package com.upc.controller;

import com.upc.pojo.Dept;
import com.upc.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 分页获取部门信息
     * @param page 当前的页数
     * @param limit 每页显示的信息数量
     * @return
     */
    @RequestMapping("getDeptByPage")
    @ResponseBody
    public HashMap getDeptByPage(int page, int limit){
        // return deptService.getDeptByPage(curPage,pageSize)
        HashMap map=new HashMap();
        //当前页的数据
        List<Dept> deptList= deptService.getDeptByPage(page,limit);
        //部门的总条数
        int totalCount=deptService.getDeptCount();
        map.put("code",0);
        map.put("msg","");
        map.put("count",totalCount);
        map.put("data",deptList);
        return map;
    }


    /**
     * 根据deptno删除对应的数据
     * @param deptno
     * @return
     */
    @RequestMapping("delDeptById")
    @ResponseBody
    public HashMap delDeptById(int deptno){
        HashMap map=new HashMap();
        int code;
        if(deptService.delDeptById(deptno)>0){
            code=0;
        }else{
            code=-1;
        }
        map.put("code",code);
        return map;
    }

    @RequestMapping("addNewData")
    @ResponseBody
    public HashMap addNewData(int deptno,String dname,String loc){
        HashMap hashMap=new HashMap();
        int code;
        if(deptService.addNewData(deptno,dname,loc)>0){
            code=0;
        }else{
            code=-1;
        }
        hashMap.put("code",code);
        return hashMap;
    }

    @RequestMapping("updateDept")
    @ResponseBody
    public HashMap updateDept(Dept dept){
        HashMap map=new HashMap();
        int code;
        if(deptService.updateDept(dept)>0){
            code=0;
        }else{
            code=-1;
        }
        map.put("code",code);
        return map;
    }



}
